package com.example.feature;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 异步有依赖的多个任务
 */
public class ThenApplyDemo {
    public static void main(String[] args) {
        //手动创建一个有100 个线程的线程池
        ExecutorService executorService = Executors.newFixedThreadPool(100);

        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(300L);
                System.out.println(Thread.currentThread().getName());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return 123;
            //thenApply依赖任务执行是同一个线程
            //thenApplyAsync 会使用一个新的线程
        }, executorService).thenApplyAsync(value->{
            System.out.println(Thread.currentThread().getName());
            int res = value * 10;
            System.out.println(res);
            return res;
        });


        executorService.shutdown();
    }
}
